//
//  LGPatientDetailAddNodeView.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/10/17.
//

#import "LGPatientDetailAddNodeView.h"
#import "DatePickerTwoView.h"

@interface LGPatientDetailAddNodeView()<DatePickerViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**  就诊类型 */
//@property (nonatomic , strong)UILabel *typeTitle;


/**  就诊日期 */
//@property (nonatomic , strong)UILabel *dateTitle;
@property (nonatomic , strong)UITextField *dateTF;
//@property (nonatomic , strong)UIButton *dateButton;


/**   */
@property (nonatomic , strong)NSArray *itemArray;
/**   */
@property (nonatomic , copy)NSString *nodeDate;
/**   */
@property (nonatomic , copy)NSString *nodeid;
/**   */
@property (nonatomic , copy)NSString *nodeType;
/**   */
@property (nonatomic , assign)CGFloat dateTop;

@end

@implementation LGPatientDetailAddNodeView

- (instancetype)initWithFrame:(CGRect)frame dataDic:(NSDictionary *)dataDic{
    self = [super initWithFrame:frame];
    if (self) {
        self.nodeType = @"";
        self.nodeid = LGNSString(dataDic[@"node_id"]);
        self.nodeDate = LGNSString(dataDic[@"node_date"]);
        self.itemArray = dataDic[@"node_items"];
        
        CGFloat dateTop = viewPix(84);
        for (NSInteger i=0; i<self.itemArray.count; i++) {
            NSDictionary *tempDic = self.itemArray[i];
            UIButton *itemBtn = [UIButton buttonWithTitle:LGNSString(tempDic[@"node_title"]) titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#606266" frame:CGRectMake(Screen_W-viewPix(248)+viewPix(118)*(i%2), viewPix(24)+viewPix(48)*(i/2), viewPix(110), viewPix(40))];
            [itemBtn setTitleColor:[UIColor colorWithString:@"#5E8EF5"] forState:UIControlStateSelected];
            [itemBtn setBackgroundColor:@"#FFFFFF" selectColor:@"#E5F1FF"];
            [itemBtn addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:2];
            itemBtn.selected = [tempDic[@"selected"] integerValue]>0?YES:NO;
            NSString *bordColor = [tempDic[@"selected"] integerValue]>0?@"#5E8EF5":@"#EEEEEE";
            itemBtn.layer.borderColor = [UIColor colorWithString:bordColor].CGColor;
            itemBtn.tag = 1000+i;
            [itemBtn addTarget:self action:@selector(typeButtonSelect:) forControlEvents:UIControlEventTouchUpInside];
            [self.baseView addSubview:itemBtn];
            dateTop = kMaxY(itemBtn.frame)+viewPix(16);
            if ([tempDic[@"selected"] integerValue]>0) {
                self.nodeType = LGNSString(tempDic[@"node_type"]);
            }
        }
        
        [self createSubViewWithTop:dateTop];
        CGFloat baseH = self.baseView.bounds.size.height;
        [UIView animateWithDuration:0.3 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            self.baseView.frame = CGRectMake(0, Screen_H-baseH+viewPix(15), Screen_W, baseH);
        }];
    }
    return self;
}

//就诊类型
-(void)typeButtonSelect:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        UIButton *itemBtn = [self viewWithTag:1000+i];
        itemBtn.selected = (i == index)?YES:NO;
        NSString *bordColor = (i == index)?@"#5E8EF5":@"#EEEEEE";
        itemBtn.layer.borderColor = [UIColor colorWithString:bordColor].CGColor;
    }
    self.nodeType = LGNSString(self.itemArray[index][@"node_type"]);
}

-(void)selectDateAction{
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.nodeDate;
    pickerView.title = @"就诊日期";
    pickerView.delegate = self;
    [[NSObject mainWindow] addSubview:pickerView];
    CGFloat baseH = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H-baseH+viewPix(15)-240, Screen_W, baseH);
    }];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    self.dateTF.text = date;
    self.nodeDate = date;
}

-(void)closeDatePickerVeiw{
    CGFloat baseH = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H-baseH+viewPix(15), Screen_W, baseH);
    }];
}

//确认
-(void)sureButtonAction{
    if ([self.nodeType isEmptyString]) {
        [TooltipView showMessage:@"请选择就诊类型" offset:0];
        return;
    }
    
    if ([self.nodeDate isEmptyString]) {
        [TooltipView showMessage:@"请选择就诊日期" offset:0];
        return;
    }
    [self closeViewAnimation];
    if (self.saveNodeData) {
        self.saveNodeData(@{@"node_id":self.nodeid,@"node_type":self.nodeType,@"node_date":self.nodeDate,@"patient_id":self.patientid,@"disease_code":self.diseaseCode});
    }
}

//取消
-(void)closeViewAnimation{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, self.baseView.bounds.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubViewWithTop:(CGFloat)dateTop{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, dateTop+viewPix(146)+bottomSafeBarHeight/2.0);
    UILabel *typeTitle = [UILabel lableWithText:@"就诊类型" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    typeTitle.frame = CGRectMake(viewPix(24), viewPix(24), viewPix(70), viewPix(40));
    [self.baseView addSubview:typeTitle];
//    [self.baseView addSubview:self.typeBtn1];
//    [self.baseView addSubview:self.typeBtn2];
    
    UILabel *dateTitle = [UILabel lableWithText:@"就诊日期" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    dateTitle.frame = CGRectMake(viewPix(24), dateTop, viewPix(70), viewPix(40));
    [self.baseView addSubview:dateTitle];
    [self.baseView addSubview:self.dateTF];
    self.dateTF.frame = CGRectMake(Screen_W-viewPix(248), dateTop, viewPix(228), viewPix(40));
    UIButton *dateButton = [UIButton buttonWithImage:@"grayTip16" frame:CGRectMake(Screen_W-viewPix(248), dateTop, viewPix(228), viewPix(40))];
    dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    dateButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(20));
    [dateButton addTarget:self action:@selector(selectDateAction) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:dateButton];
    
    UIButton *cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#5E8EF5" frame:CGRectMake(viewPix(16), kMaxY(self.dateTF.frame)+viewPix(30), (Screen_W-viewPix(44))/2.0, viewPix(44))];
    [cancelBtn addBorder:@"#5E8EF5" lineWidth:1 cornerRidus:viewPix(22)];
    [cancelBtn addTarget:self action:@selector(closeViewAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:cancelBtn];
    
    UIButton *sureButton = [UIButton buttonWithTitle:@"确认" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" frame:CGRectMake(kMaxX(cancelBtn.frame)+viewPix(12), kMinY(cancelBtn.frame), (Screen_W-viewPix(44))/2.0, viewPix(44))];
    sureButton.backgroundColor = [UIColor colorWithString:@"#5E8EF5"];
    sureButton.cornerRidus = viewPix(22);
    [sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:sureButton];
    
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(230)+bottomSafeBarHeight/2.0)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}

-(UITextField *)dateTF{
    if(!_dateTF){
        _dateTF = [[UITextField alloc]initWithFrame:CGRectMake(Screen_W-viewPix(248), viewPix(84), viewPix(228), viewPix(40))];
        _dateTF.tintColor = [UIColor colorWithString:@"#606266"];
        _dateTF.textColor = [UIColor colorWithString:@"#606266"];
        _dateTF.textAlignment = NSTextAlignmentLeft;
        _dateTF.keyboardType = UIKeyboardTypeDefault;
        _dateTF.placeholder = @"请选择";
        _dateTF.font = LGFontWeight(14, UIFontWeightMedium);
        _dateTF.userInteractionEnabled = NO;
        _dateTF.text = self.nodeDate;
        _dateTF.leftViewMode = UITextFieldViewModeAlways;
        _dateTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(20), viewPix(40))];
        [_dateTF addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:2];
    }
    return _dateTF;
}





@end
